﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ToppingControl.Models;
using WinNemoLib;

namespace ToppingControl.Controllers
{
    public class CPD_USEController : Controller
    {
        //
        // GET: /CPD_USE/

      
        public ActionResult Index()
       {
          List<CompoundUseModel.CompoundUse_DS> list = new List<CompoundUseModel.CompoundUse_DS>();
          CompoundUseModel mdSearch = new CompoundUseModel();
          list = mdSearch.SEL_TOP_COMPUSED(WinNemoLib.DateUtil.ToDisplayDate(DateTime.Now, WinNemoLib.DateUtil.eLanguage.EN));

           return View(list);
        }

        public JsonResult Get_CPD()
        {
           TopCordBomModel cpd = new TopCordBomModel();
           string[] s = cpd.SEL_COMPOUND();
           return Json(s);
        }

        public JsonResult Get_TOPCRDCD()
        {
           TopCordBomModel cpd = new TopCordBomModel();
           string[] s = cpd.SEL_TOPCRDCD();
           return Json(s);
        }

        public JsonResult Save_Data(CompoundUseModel.CompoundUse_DS theData)
        {

           CompoundUseModel mdUpd = new CompoundUseModel();

           if (mdUpd.Ins_Data(theData))
           {
              return Json(new { success = 1, message = "บันทึกเรียบร้อย" });
           }
           else
           {
              return Json(new { success = 0, message = "ไม่สามารถบันทึกได้" });
           }

        }


        public ActionResult CPC_CHANGE(string seq)
        {
       
          
            string[] seqs = seq.Split(new string[] { "-" }, StringSplitOptions.RemoveEmptyEntries);

            List<TopCordPlanModel.TopCordPlan_DS> list = new List<TopCordPlanModel.TopCordPlan_DS>();
            TopCordPlanModel md = new TopCordPlanModel();
            list = md.SEL_TOPCORDPLN("", seqs[0],"", " top 1 ", seqs[1]);

            if (list.Count() <= 0)
            {
                return View(new TopCordPlanModel.TopCordPlan_DS());
            }
            ViewBag.shift = list[0]._Shift;
            return View(list[0]);   
        }

        public JsonResult Save_Change_CPD(List<TopCordPlanModel.TopCordPlan_DS> theData)
        {
         
            TopCordActModel mdTpln = new TopCordActModel();
            string roll = mdTpln.GET_ROLLNO(theData[0]._Plandt,theData[0]._Itemno);
            if (StrUtil.IsEmpty(roll))
            {
                return Json(new { success = 0, message = "มีข้อมูลการเปลี่ยนแปลงแล้ว" });
            }

            CompoundUseModel mdUpd = new CompoundUseModel();

            if (mdUpd.Ins_CPD_CH(theData[0], roll))
            {
                return Json(new { success = 1, message = "บันทึกเรียบร้อย" });
            }
            else
            {
                return Json(new { success = 0, message = "ไม่สามารถบันทึกได้" });
            }

        }

    }
}
